"an accordion segment"

from whiff.middleware import misc
from whiff import gateway

TEMPLATE = """
<div id="%s">%s</div>
<div id="%s">%s</div>
"""

IDENTITIES_VARIABLE = "mooAccordion.identities"

class mooSegment(misc.utility):
    def __init__(self,
                 toggler,
                 text,
                 variable=IDENTITIES_VARIABLE,
                 ):
        self.toggler = toggler
        self.segment = text
        self.variable = variable
    def __call__(self, env, start_response):
        variable = self.param_value(self.variable, env)
        pairs = env[variable]
        toggler = self.param_value(self.toggler, env)
        segment = self.param_value(self.segment, env)
        togglerid = gateway.getResource(env, ["freshName", "toggler"])
        segmentid = gateway.getResource(env, ["freshName", "segment"])
        # store the ids for use by the accordion javascript generator
        pairs.append( (togglerid, segmentid) )
        # generate the html fragment for this segment/toggler
        htmlOut = TEMPLATE % (togglerid, toggler, segmentid, segment)
        return self.deliver_page(htmlOut, env, start_response)

__middleware__ = mooSegment
